layui.define(["table", "form", "upload", "admin"], function (exports) {
    var $ = layui.$,
        table = layui.table,
        form = layui.form,
        upload = layui.upload,
        admin = layui.admin;

    table.render({
        elem: "#LAY-help-manage",
        url: "/admin/help/getlist",
        cols: [[
            {field: "id", width: 100, title: "ID", sort: true, fixed: "left"}
            , {field: 'title', title: '标题', minWidth: 150}
            , {field: 'type_txt', title: '分类', minWidth: 150}
            , {
                field: '', title: '图标', width: 80, templet: function (d) {
                    if (d.icon) {
                        return '<img style="display: inline-block;height: 100%;" src= "' + d.icon + '" layadmin-event="iconPreview" value= "' + d.icon + '">';
                    }
                    return '';
                }
            }
            , {field: 'weight', title: '权重', minWidth: 150}
            , {title: '操作', width: 120, align: 'center', fixed: 'right', toolbar: '#table-helplist'}
        ]],
        page: false,
        height: "full-140",
        text: "对不起，加载出现异常！",
        even: true,
    });

    //监听工具条
    table.on("tool(LAY-help-manage)", function (obj) {
        if (obj.event === "edit") {
            var index = layer.open({
                type: 2,
                title: "编辑帮助中心",
                content: "/admin/help/edit?id=" + obj.data.id,
                maxmin: true,
                area: ["600px", "550px"],
            });
            layer.full(index);
        }
        if (obj.event === "open") {
            window.open(obj.data.url);
        }
    });


    //上传头像
    var iconSrc = $('#LAY_iconSrc');
    var iconPreview = $('#LAY_iconPreview');
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '#LAY_iconUpload'
        , auto: true
        , choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/index/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                console.log(err);
                                layer.msg("上传失败", {icon: 5});
                            } else {
                                if (data.statusCode == 200) {
                                    iconSrc.val("https://" + data.Location);
                                    iconPreview.attr("value", iconSrc.val());
                                } else {
                                    layer.msg("图片上传失败", {icon: 5});
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            })
        }
    });

    //查看头像
    admin.events.iconPreview = function (othis) {
        var src = othis.attr("value");
        if (!src) {
            src = iconSrc.val();
        }
        layer.photos({
            photos: {
                "title": "查看" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };


    exports("help", {});
});
